﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Drawing;
//using System.Drawing.Drawing2D;

public class FM_Tie
{
    public FM_Note s, e;
    public FM_Score score;
    public int up = 0; // if 0, then follow the stems. If 1, then down. If 2, then up.

    public FM_Tie(FM_Score score)
    {
        s = null;
        e = null;
        this.score = score;
        this.up = 0;
    }

    public FM_Tie(FM_Score score, bool up)
    {
        s = null;
        e = null;
        this.score = score;
        this.up = up ? 2 : 1;
    }

    public void AddStart(FM_Note s)
    {
        this.s = s;
    }

    public void AddEnd(FM_Note e)
    {
        this.e = e;
    }

    public void Draw(ScoreCanvas graphics)//(Graphics graphics)
    {
        if (!s.blurred && !s.visible) return;
        //if (s.IsBlurred())
        //    score.Font.SetMaskFilter(new BlurMaskFilter(15, BlurMaskFilter.Blur.Normal));
        //else
        //    score.Font.SetMaskFilter(null);

        float x = s.StartX + s.paddingLeft + s.WidthAccidental() + s.paddingNote + s.WidthNote();
        float xe = e.StartX + e.paddingLeft + e.WidthAccidental() + e.paddingNote;
        float y = s.ys + (s.GetDisplacement() + 0.5f) * score.GetDistanceBetweenStaveLines();
        float ye = e.ys + (e.GetDisplacement() + 0.5f) * score.GetDistanceBetweenStaveLines();

        bool drawUp = s.stem_up;
        if (up == 1) drawUp = true;
        if (up == 2) drawUp = false;

        GraphicPath path;
        float lineWidth = 1f;
        x += 2 * FM_Const.GetScaleFactor();

        if (x > xe && ye > y) // if the notes are on different lines
        {
            float e1 = this.s.WidthNote() * 2;
            if (drawUp)
            {
                path = new GraphicPath();
                path.Reset();
                path.MoveTo(x, y);
                path.CubicTo(x, y, (2 * x + e1) / 2, y + score.GetDistanceBetweenStaveLines() / 2, x + e1, y + score.GetDistanceBetweenStaveLines() / 2);
                path.CubicTo(x + e1, y + score.GetDistanceBetweenStaveLines() / 2, (2 * x + e1) / 2, y + score.GetDistanceBetweenStaveLines(), x, y);
            }
            else
            {
                y = y - score.GetDistanceBetweenStaveLines();
                path = new GraphicPath();
                path.Reset();
                path.MoveTo(x, y);
                path.CubicTo(x, y, (2 * x + e1) / 2, y - score.GetDistanceBetweenStaveLines() / 2, x + e1, y - score.GetDistanceBetweenStaveLines() / 2);
                path.CubicTo(x + e1, y - score.GetDistanceBetweenStaveLines() / 2, (2 * x + e1) / 2, y - score.GetDistanceBetweenStaveLines(), x, y);
            }
            graphics.DrawPath(path, lineWidth);

            float e2 = this.e.WidthNote() * 2;
            if (drawUp)
            {
                path = new GraphicPath();
                path.Reset();
                path.MoveTo(xe, ye);
                path.CubicTo(xe, ye, (2 * xe - e2) / 2, ye + score.GetDistanceBetweenStaveLines() / 2, xe - e2, ye + score.GetDistanceBetweenStaveLines() / 2);
                path.CubicTo(xe - e2, ye + score.GetDistanceBetweenStaveLines() / 2, (2 * xe - e2) / 2, ye + score.GetDistanceBetweenStaveLines(), xe, ye);
            }
            else
            {
                ye = ye - score.GetDistanceBetweenStaveLines();
                path = new GraphicPath();
                path.Reset();
                path.MoveTo(xe, ye);
                path.CubicTo(xe, ye, (2 * xe - e2) / 2, ye - score.GetDistanceBetweenStaveLines() / 2, xe - e2, ye - score.GetDistanceBetweenStaveLines() / 2);
                path.CubicTo(xe - e2, ye - score.GetDistanceBetweenStaveLines() / 2, (2 * xe - e2) / 2, ye - score.GetDistanceBetweenStaveLines(), xe, ye);
            }
        }
        else // if the notes are on the same line
        {
            if (drawUp)
            {
                path = new GraphicPath();
                path.Reset();
                path.MoveTo(x, y);
                path.CubicTo(x, y, (xe + x) / 2, y + score.GetDistanceBetweenStaveLines() / 2, xe, ye);
                path.CubicTo(xe, ye, (xe + x) / 2, y + score.GetDistanceBetweenStaveLines(), x, y);
            }
            else
            {
                path = new GraphicPath();
                y = y - score.GetDistanceBetweenStaveLines();
                ye = ye - score.GetDistanceBetweenStaveLines();
                path.Reset();
                path.MoveTo(x, y);
                path.CubicTo(x, y, (xe + x) / 2, y - score.GetDistanceBetweenStaveLines() / 2, xe, ye);
                path.CubicTo(xe, ye, (xe + x) / 2, y - score.GetDistanceBetweenStaveLines(), x, y);
            }
        }

        graphics.DrawPath(path, lineWidth);

       // score.Font.SetMaskFilter(null);
    }
}
